package com.service.bracelet.common;

/**
 * 用于通用返回结果
 * @param <T>
 */
public class CommonResult<T> {

    public int code;

    protected String message;

    protected T data;

    public CommonResult<T> setError(CommonResultCode resultCode, String message) {
        this.code = resultCode.code;
        this.message = message;
        return this;
    }

    public CommonResult<T> setCode(CommonResultCode resultCode) {
        this.code = resultCode.code;
        return this;
    }

    public int getCode() {
        return code;
    }

    public CommonResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public CommonResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public CommonResult<T> setData(T data) {
        this.code = CommonResultCode.SUCCESS.code;
        this.data = data;
        return this;
    }

    public static CommonResult success() {
        return new CommonResult().setCode(CommonResultCode.SUCCESS);
    }

    public static CommonResult success(Object data) {
        return new CommonResult().setData(data);
    }

    public static CommonResult error(CommonResultCode resultCode, String message) {
        return new CommonResult().setError(resultCode, message);
    }

    public static CommonResult error(Exception e) {
        e.printStackTrace();
        return new CommonResult().setError(CommonResultCode.SERVER_ERROR, e.getMessage());
    }
}
